package com.nightpiter.controller.newnet;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;

import android.content.Context;
import android.os.Handler;

import com.nightpiter.R;

/**
 * @author Eldar Abusalimov
 * 
 */
public class Resource {

	/**
	 * @author Eldar Abusalimov
	 * 
	 */
	public enum Type {
		MAP, DRAWER
	}

	private Type type;

	private String name = "";

	private String url = "";

	private String filePath = "";

	private String description = "";

	Resource(Type type) {
		this.type = type;
	}

	public Type getType() {
		return type;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the filePath
	 */
	public String getFilePath() {
		return filePath;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	void setName(String name) {
		this.name = name;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String toString() {
		return name;
	}

	public void download(final Context context, final Listener listener) {
		final Handler handler = new Handler();
		(new Thread() {
			public void run() {
				try {
					handler.post(new Runnable() {
						public void run() {
							if (listener != null) {
								listener.onDownloadStart();
							}
						}
					});
					String[] parts = Resource.this.getUrl().split("/");
					String path = parts[parts.length - 1];
					if (Resource.this.getType() == Type.MAP) {
						File dir = new File(context
								.getString(R.string.maps_dir));
						if (!dir.exists()) {
							dir.mkdir();
						}
						path = context.getString(R.string.maps_dir) + "/"
								+ path;
					} else if (Resource.this.getType() == Type.DRAWER) {
						File dir = new File(context
								.getString(R.string.drawers_dir));
						if (!dir.exists()) {
							dir.mkdir();
						}
						path = context.getString(R.string.drawers_dir) + "/"
								+ path;
					}
					Resource.this.filePath = path;

					URL url = new URL(Resource.this.url);
					InputStreamReader isr = new InputStreamReader(url
							.openStream());
					BufferedReader br = new BufferedReader(isr);

					FileOutputStream fos = new FileOutputStream(path);
					OutputStreamWriter osw = new OutputStreamWriter(fos);
					BufferedWriter bw = new BufferedWriter(osw);

					int c = 0;
					while ((c = br.read()) != -1) {
						bw.write(c);
					}
					br.close();
					bw.close();

					handler.post(new Runnable() {
						public void run() {
							if (listener != null) {
								listener.onDownloadCompleted(Resource.this);
							}
						}
					});
				} catch (Exception e) {
					handler.post(new Runnable() {
						public void run() {
							if (listener != null) {
								listener.onDownloadFailed();
							}
						}
					});

				}
			}
		}).start();

	}

	/**
	 * 
	 * @author Eldar Abusalimov
	 * 
	 */
	public static interface Listener {

		public void onDownloadStart();

		public void onDownloadCompleted(Resource resource);

		public void onDownloadFailed();

	}

}
